/**
 * @ClassName: SysProductPriceDao
 * @author linsen
 * @date 2016-10-12
 */
package com.fzrskj.cloud.free.sys.dao;

import com.fzrskj.cloud.free.core.utils.CloudContextUtils;
import com.fzrskj.cloud.free.core.utils.CloudDbDrvUtils;
import com.fzrskj.cloud.free.sys.bean.SysProductPrice;
import ms.core.consts.SysErr;
import ms.core.db.IDbDrv;
import ms.core.object.CObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SysProductPriceDao extends CObject{
	//private declarations
	private IDbDrv db=null;

	//constructor function to initialize database access object
	public SysProductPriceDao(){
		db=CloudDbDrvUtils.getDbUtilsDrv();
	}

	//to free resource
	@Override
	public void dispose(){
		db.closeConnection();
	}

	//to add new record to database by bean
	public int addSysProductPrice(SysProductPrice obj){
		Map<String, Object> values = new HashMap<>();
		values.put("productClass", obj.getProductClass());
		values.put("productCode", obj.getProductCode());
		values.put("regType", obj.getRegType());
		values.put("chargeId", obj.getChargeId());
		values.put("levelCode", obj.getLevelCode());

		values.put("agentId", CloudContextUtils.getAgentId());

		if (obj.getRegType() != null){
			values.put("regType", obj.getRegType());
		}
		if (obj.getPrice1() >= 0){
			values.put("price1", obj.getPrice1());
		}
		if (obj.getPrice2() >= 0){
			values.put("price2", obj.getPrice2());
		}
		if (obj.getPrice3() >= 0){
			values.put("price3", obj.getPrice3());
		}
		if (obj.getPrice4() >= 0){
			values.put("price4", obj.getPrice4());
		}
		if (obj.getPrice5() >= 0){
			values.put("price5", obj.getPrice5());
		}
		if (obj.getPrice6() >= 0){
			values.put("price6", obj.getPrice6());
		}
		if (obj.getPrice7() >= 0){
			values.put("price7", obj.getPrice7());
		}
		if (obj.getPrice8() >= 0){
			values.put("price8", obj.getPrice8());
		}
		if (obj.getPrice9() >= 0){
			values.put("price9", obj.getPrice9());
		}
		if (obj.getPrice10() >= 0){
			values.put("price10", obj.getPrice10());
		}

		int ret = db.insertRecord("sys_product_price", values);
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	private Object getPrice(int price) {
		return price < 0 ? null : price + "";
	}

	//to add new record to database by map
	public int addSysProductPrice(Map<String, Object> values) {

		values.put("agentId", CloudContextUtils.getAgentId());

		int ret = db.insertRecord("sys_product_price", values);
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	//to delete table record by id
	public int delSysProductPrice(int id) {
		String sql = "delete from sys_product_price where id=? and agentId=?";
		int ret = db.executeSql(sql, new Object[]{id, CloudContextUtils.getAgentId()});
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	//to delete table record by cdt
	public int delSysProductPrice(Map<String, Object> cdt) {

		cdt.put("agentId", CloudContextUtils.getAgentId());

		int ret = db.deleteRecord("sys_product_price", cdt);
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	//to delete table record by cdt
	public int delSysProductPrice(String productCode) {
		String sql = "delete from sys_product_price where ISNULL(regType) and productCode=? and agentId=?";
		int ret = db.executeSql(sql, new Object[]{productCode, CloudContextUtils.getAgentId()});
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	//to delete table record by cdt
	public int delSysDomainProductPriceNotDefaule(String productClass, String productCode) {
		String sql = "delete from sys_product_price where !ISNULL(regType) and productClass=? and productCode=? and agentId=?";
		int ret = db.executeSql(sql, new Object[]{productClass, productCode, CloudContextUtils.getAgentId()});
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	//to modify table record by values and condition
	public int modSysProductPrice(Map<String, Object> values, Map<String, Object> cdt){

		cdt.put("agentId", CloudContextUtils.getAgentId());

		int ret = db.modifyRecord("sys_product_price", values, cdt);
		String code = db.getErrCode();
		this.setErrCode(code);
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}

		return ret;
	}

	//to query sys_product_price item for detail
	public SysProductPrice querySysProductPrice(int id) {
		String sql = "select * from sys_product_price where id=? and agentId=?";
		SysProductPrice ret = db.queryForBean(sql, SysProductPrice.class, new Object[]{id, CloudContextUtils.getAgentId()});
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	//to query SysProductprice record count
	public int querySysProductPrice(Map<String, Object> cdt){

		cdt.put("agentId", CloudContextUtils.getAgentId());

		int ret = db.queryRecordCount("sys_product_price", cdt);
		this.setErrCode(db.getErrCode());
		return ret;
	}

	//to query SysProductprice for record list with condition,if page or pagesize less than 0, means without page
	public List<SysProductPrice> querySysProductPriceList(Map<String, Object> cdt, int page, int pagesize){

		cdt.put("agentId", CloudContextUtils.getAgentId());

		List<SysProductPrice> ret = db.queryForBeans("sys_product_price", "*", "id desc", SysProductPrice.class, cdt, page, pagesize);
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}
	
	//自定义方法(非生成)
	//获取产品价格列表
	public List<SysProductPrice> getSysProductPriceList(String productCode) {
		String sql = "select * from sys_product_price where productCode=? and agentId=?";
		Object[] params = {productCode, CloudContextUtils.getAgentId()};
		List<SysProductPrice> list = db.queryForBeans(sql, SysProductPrice.class, params);
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return list;
	}

	//获取产品价格
	public int getProductPrice(int year, Map<String, Object> cdt) {
		if (!cdt.containsKey("agentId")) {
			cdt.put("agentId", CloudContextUtils.getAgentId());
		}

		int price = 0;
		StringBuilder sb = new StringBuilder(String.format("select price%s ", year));
		sb.append("from sys_product_price where 1=1");
		int i = 0;
		Object[] params = new Object[cdt.size()];
		for (Map.Entry<String, Object> c : cdt.entrySet()) {
			sb.append(" and " + c.getKey() + "=?");
			params[i] = c.getValue();
			i++;
		}

		Object result = db.queryFieldValue(sb.toString(), double.class, params, 1);
		if (result != null) {
			price = Integer.parseInt(result.toString());
		}
		this.setErrCode(db.getErrCode());
		return price;
	}

	//获取产品价格(域名)
	public int getDomainProductPrice(int year, Map<String, Object> cdt) {

		cdt.put("agentId", CloudContextUtils.getAgentId());

		int price = -1;
		StringBuilder sb = new StringBuilder(String.format("select price%s ", year));
		sb.append("from sys_product_price where 1=1");
		int i = 0;
		Object[] params = new Object[cdt.size()];
		for (Map.Entry<String, Object> c : cdt.entrySet()) {
			sb.append(" and " + c.getKey() + "=?");
			params[i] = c.getValue();
			i++;
		}

		Object result = db.queryFieldValue(sb.toString(), double.class, params, 1);
		if (result != null) {
			price = Integer.parseInt(result.toString());
		}
		this.setErrCode(db.getErrCode());
		return price;
	}

	//获取产品可购买时长
	public SysProductPrice getProductBuyTime(Map<String, Object> cdt) {

		if (!cdt.containsKey("agentId")) {
			cdt.put("agentId", CloudContextUtils.getAgentId());
		}

		StringBuilder sb = new StringBuilder("select * ");
		sb.append("from sys_product_price  where 1=1 and ISNULL(regType)");

		int i = 0;
		Object[] params = new Object[cdt.size()];
		for (Map.Entry<String, Object> c : cdt.entrySet()) {
			sb.append(" and " + c.getKey() + "=?");
			params[i] = c.getValue();
			i++;
		}

		SysProductPrice view = db.queryForBean(sb.toString(), SysProductPrice.class, params);
		String err = db.getErrCode();
		if (!err.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		this.setErrCode(err);
		return view;
	}
	//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓代理和主站数据互通专用↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

	/**
	 * 查询产品价格配置（平台与代理数据同步使用）
	 *
	 * @param productClass 产品类型
	 * @param productCode 产品编码
	 * @param agentId 代理标识
	 * @return
	 */

	public List<SysProductPrice> queryAgentSysProductPrice(String productClass, String productCode, int agentId){

		String sql = "select * from sys_product_price where productClass=? and productCode=? and agentId=? ";
		List<SysProductPrice> ret = db.queryForBeans(sql, SysProductPrice.class, new Object[]{productClass,productCode,agentId});
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	/**
	 * 新增价格配置（平台与代理数据同步使用）
	 *
	 * @param obj 价格Bean
	 * @return
	 */
	public int addAgentSysProductPrice(SysProductPrice obj){
		if (obj.getAgentId() == null){
			return -1;
		}
		if (obj.getAgentId() < 0){
			return -1;
		}
		Map<String, Object> values = new HashMap<>();
		values.put("productClass", obj.getProductClass());
		values.put("productCode", obj.getProductCode());
		values.put("regType", obj.getRegType());
		values.put("chargeId", obj.getChargeId());
		values.put("levelCode", obj.getLevelCode());

		values.put("agentId", obj.getAgentId());

		if (obj.getPrice1() >= 0){
			values.put("price1", obj.getPrice1());
		}
		if (obj.getPrice2() >= 0){
			values.put("price2", obj.getPrice2());
		}
		if (obj.getPrice3() >= 0){
			values.put("price3", obj.getPrice3());
		}
		if (obj.getPrice4() >= 0){
			values.put("price4", obj.getPrice4());
		}
		if (obj.getPrice5() >= 0){
			values.put("price5", obj.getPrice5());
		}
		if (obj.getPrice6() >= 0){
			values.put("price6", obj.getPrice6());
		}
		if (obj.getPrice7() >= 0){
			values.put("price7", obj.getPrice7());
		}
		if (obj.getPrice8() >= 0){
			values.put("price8", obj.getPrice8());
		}
		if (obj.getPrice9() >= 0){
			values.put("price9", obj.getPrice9());
		}
		if (obj.getPrice10() >= 0){
			values.put("price10", obj.getPrice10());
		}

		int ret = db.insertRecord("sys_product_price", values);
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	/**
	 * 删除产品价格（平台与代理数据同步使用）
	 *
	 * @param cdt 删除条件
	 * @return
	 */
	public int delAgentSysProductPrice(Map<String, Object> cdt) {
		String code = SysErr.SYS_PARAM_ERR;
		int ret = -1;
		do {
			//没有agentId 删除
			if (!cdt.containsKey("agentId")) {
				code = SysErr.SYS_PARAM_ERR;
				break;
			}
			//没有agentId 小于0删除
			if (Integer.valueOf(cdt.get("agentId").toString()) < 0) {
				code = SysErr.SYS_PARAM_ERR;
				break;
			}
			ret = db.deleteRecord("sys_product_price", cdt);
			code = db.getErrCode();
			if (!code.equals(SysErr.CODE_SUCCESS)) {
				this.log(db.getMsg());
			}
		} while (false);
		this.setErrCode(code);
		return ret;
	}

	//↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑代理和主站数据互通专用↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
}
